<?php
/**
 * Layout.php
 *
 * @category 	Madoqua
 * @package 	Controller
 * @subpackage 	Plugin
 */

/** base class */
require_once 'Zend/Controller/Plugin/Abstract.php';

/** zend loader */
require_once 'Zend/Loader.php';

/**
 * Madoqua_Controller_Plugin_Layout
 *
 * @category 	Madoqua
 * @package 	Controller
 * @subpackage 	Plugin
 */
class Madoqua_Controller_Plugin_Layout extends Zend_Controller_Plugin_Abstract {

    /**
     * active theme
     *
     * @var string
     */
    protected $_activeTheme = 'default';

    /**
     * do layout?
     *
     * @var bool
     */
    protected $_doLayout = true;

    /**
     * constructor
     */
    public function __construct()
    {
        $config = Madoqua_Application::getConfiguration();
        //application config

        $this->_activeTheme = $config->theme->active;
        //the active theme
    }

    /**
     * dispatch loop startup
     *
     * we're working under the assumption that once a user starts a request to an admin module,
     * he stays there.
     *
     * @param  Zend_Controller_Request_Abstract $request
     * @return void
     */
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        Zend_Layout::startMvc(array(
            'layoutPath' => Madoqua_Application::getApplicationDirectory() . DIRECTORY_SEPARATOR .  'layouts/scripts/',
            'layout' => 'front/index'
        ));
        //start the layout
        //use front by default

        if ($request->getModuleName() == 'install') {
            Zend_Layout::getMvcInstance()->setLayout('install/install');
            //use install layout
        } elseif ($request->getModuleName() == 'auth') {
            Zend_Layout::getMvcInstance()->setLayout('admin/login');
            //use install layout
        } elseif (in_array($request->getModuleName(), Madoqua_Application::getAdminModules())) {
            //admin module (or install)
            Zend_Layout::getMvcInstance()->setLayout('admin/index');
            //set admin layout for... admin modules
        } else {
            //we're not in aa "special" module, set custom layout

            $path = Madoqua_Application::getDataDirectory() . DIRECTORY_SEPARATOR  . 'themes/';
            $path .=  $this->_activeTheme . '/layouts/';
            //the path to the selected theme layouts

            Zend_Layout::getMvcInstance()->setLayoutPath($path);
            //set it as the path

            Zend_Layout::getMvcInstance()->setLayout('index');
            //index layout is the default (can be overridden on view script level)
        }
    }

    /**
     * dispatch loop shutdown
     *
     * @return void
     */
    public function dispatchLoopShutdown()
    {
        /* Issue #76
           Lets just get the hell out of here if we have a 404 or 500 response
           There's a strong chance the exception may bubble up and get
           thrown/sent to the browser as plain text, this causes the browser to
           barf when its mixed with the gzipped output.
        */
        $code = $this->_response->getHttpResponseCode();
        if($code == 404 || $code = 500) {
            return;
        }

        $doGzip = false;
        //let's assume we don't want it :x

        $headers = $this->getResponse()->getRawHeaders();
        //all the raw headers

        foreach($headers as $header) {
            $header = strtolower($header);

            if (strpos($header, 'content-type: ') == 0) {
                //content type header
                $contentType = substr($header, 14, strpos($header, ';'));
            }
        }
        //try to find content type

        if (!empty($contentType) && strpos($contentType, 'text/') == 0) {
            $doGzip = true;
        }
        //if we have a content type that starts with text/ we can assume we want gzip
        //a bit basic... but effective :x

        if ($doGzip && function_exists('gzencode')) {
            //we should do gzip encoding

            $content = $this->getResponse()->getBody();
            $this->getResponse()->setBody(gzencode($content));
            //encode content

            $this->getResponse()->setHeader('Content-Encoding', 'gzip', true);
            //set header to notify browser to expect gzip-ed content
        }
    }
}